import 'package:flutter/material.dart';
import 'package:test_flutter/model/response/service_model.dart';
import 'package:test_flutter/pages/dryingDetailPage.dart';
import 'package:test_flutter/utils/factory/Fit.dart';
import 'package:test_flutter/utils/overLay_util.dart';
import 'package:test_flutter/widgets/DryingHero.dart';
import 'package:test_flutter/dio/DioUtil.dart';
import 'package:test_flutter/dio/Api.dart';
import 'package:test_flutter/utils/ToastUtil.dart';


bool hasInit = false;


class DryingPage extends StatefulWidget {
  DryingPage({Key key}) : super(key: key);
  @override
  _DryingPage createState() => new _DryingPage();
}


var list = new List();


class _DryingPage extends State<DryingPage> {
  //页面初始化状态的方法

// 这个key用来在不是手动下拉，而是点击某个button或其它操作时，代码直接触发下拉刷新
  final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
      new GlobalKey<RefreshIndicatorState>();


  @override
  void initState() {
    super.initState();
    if (!hasInit) {
      _getDefaultPolicy();
    }

    hasInit = true;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: RefreshIndicator(
        key: _refreshIndicatorKey,
        onRefresh: _pullToRefresh,
        child: ListView.builder(
          padding: EdgeInsets.only(
              left: Fit.scaleX(30),
              top: Fit.scaleY(40),
              right: Fit.scaleX(30),
              bottom: Fit.scaleY(40)),
          itemCount: list.length,
          itemBuilder: (BuildContext context, int position) {
            return getItem(position);
          },
        ),
      ),
    );
  }

  getItem(int position) {
    Map<String, dynamic> bean = list[position];
    var row = GestureDetector(
      onTap: () {
        Navigator.push<Object>(context,
            new MaterialPageRoute(builder: (BuildContext context) {
          return new DryingDetailPage(position, ServiceModel.fromJson(bean));
        }));
      },
      child: new DryingHero(position, ServiceModel.fromJson(bean)),
    );
    return Card(
      elevation: Fit.scaleX(20),
      child: row,
    );
  }

  Future<Null> _pullToRefresh() async {
    _getDefaultPolicy();
    return null;
  }

  ///获取洗衣策略列表
  _getDefaultPolicy() async {
    DioUtil.getInstance(context).get(Api.getDefaultPolicy + '2', (response) {
      if (response.data['code'] == 1) {
        list = response.data['data']['service'];
        setState(() {
          //list = response.data['data']['list'];
        });
      } else {
        ToastUtil.showToast(response.data['msg']);
      }

    }, errorCallBack: (errorMsg) {
      print("error=========================:" + errorMsg);
      ToastUtil.showToast(errorMsg);
    });
  }
}
